﻿using System.Threading;
using System.Threading.Tasks;
using Missbot.Common.Net;
using Missbot.Model.MsgCmds;

namespace Missbot.MisskeyClient
{
    public class MisskeyClient
    {
        public MisskeyClientMeta Meta { get; }

        public MisskeyClient(MisskeyClientMeta Meta)
        {
            this.Meta = Meta;
        }

        #region 发送命令

        /// <summary>
        /// 发送命令（无返回值）
        /// </summary>
        /// <param name="cmd"></param>
        public void SendCmd(IMsgCommand cmd)
        {
            cmd.I = Meta.I;
            HttpRequestor.PostData<object>(cmd.Url, cmd);
        }

        public void SendCmdDelay(IMsgCommand cmd, int delaySeconds)
        {
            Task.Run(() =>
                     {
                         Thread.Sleep(delaySeconds * 1000);
                         SendCmd(cmd);
                     });
        }

        /// <summary>
        /// 发送命令（需要返回值）
        /// </summary>
        /// <param name="cmd"></param>
        /// <typeparam name="RespType"></typeparam>
        /// <returns></returns>
        public RespType SendCmdWithResp<RespType>(IMsgCommand cmd) where RespType : class
        {
            cmd.I = Meta.I;
            return HttpRequestor.PostData<RespType>(cmd.Url, cmd);
        }
        
        #endregion
    }
}